package uit.ce03.game_ninja.MainGame;

import uit.ce03.game_ninja.NinjaActivity;
import uit.ce03.game_ninja.R;
import uit.ce03.game_ninja.Static.Static_Const;
import uit.ce03.game_ninja.database.Database;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class ChienThang extends Activity{
	int DIEM = 0;
	private Database db;
	MediaPlayer mp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.chienthang);
		 
		mp = MediaPlayer.create(this, R.raw.nhac_nen_game_1);
	        if(Static_Const.AmThanh)
	        {
	        	mp.start();
	        	mp.setLooping(true);
	        }
		
		Bundle bun = getIntent().getExtras();
		
		if(bun != null){
			DIEM = bun.getInt("diem");
		}
		db = new Database(this);
		
		Button bt_finish = (Button)findViewById(R.id.button_finish);
		bt_finish.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(db.kt_luu(DIEM)){
					Intent i = new Intent(ChienThang.this, Luudiem.class);
					i.putExtra("diem", DIEM);
					startActivity(i);
					mp.stop();
					finish();	
				}else {
					Intent i = new Intent(ChienThang.this, NinjaActivity.class);
					i.putExtra("diem", DIEM);
					startActivity(i);
					mp.stop();
					finish();	
				}
			}
		});
	}
	 @Override
		public boolean onKeyDown(int keyCode, KeyEvent event) {
			if(keyCode == KeyEvent.KEYCODE_BACK){
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(ChienThang.this);
				alertDialog.setTitle("Exit");
				alertDialog.setMessage("Bạn Có Muốn Thoát Ra khỏi Game Không ?");
				alertDialog.setIcon(R.drawable.icon_exit);
				alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						mp.stop();
						finish();
						
					}
				});
				alertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.cancel();
					}
				});
				alertDialog.show();
			}
			return super.onKeyDown(keyCode, event);
		}
	
}
